package com.qqtech.qquan.hi.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.hi.dao.HiRuleDao;
import com.qqtech.qquan.hi.dao.HiUserTaskDao;
import com.qqtech.qquan.hi.enums.HiRuleClassifyDictEnum;
import com.qqtech.qquan.hi.enums.HiRuleTaskTypeDictEnum;
import com.qqtech.qquan.hi.model.HiRuleQuery;
import com.qqtech.qquan.hi.model.HiRuleVo;
import com.qqtech.qquan.hi.model.HiUserTask;
import com.qqtech.qquan.hi.model.HiUserTaskQuery;
import com.qqtech.qquan.hi.model.HiUserTaskVo;

@Service
public class HiUserTaskServiceImpl extends BaseServiceImpl<HiUserTask>
		implements HiUserTaskService {
	public static final Logger log = LoggerFactory.getLogger(HiUserTaskServiceImpl.class);
	
	@Resource
	private HiRuleDao hiRuleDao;
	
	@Resource
	private HiUserTaskDao hiUserTaskDao;

	@Override
	protected BaseDao<HiUserTask> getBaseDao() {
		return hiUserTaskDao;
	}

	@Override
	public int finishOneTask(Integer userId, Integer ruleId) {
		return hiUserTaskDao.finishOneTask(userId, ruleId);
		
	}
	
	@Override
	public boolean isFinishAllTask(Integer userId) {
		HiUserTaskQuery taskQ = new HiUserTaskQuery();
		taskQ.setUserId(userId);
		int taskCount = hiUserTaskDao.queryCount(taskQ);
		taskQ.setIsFinish(YesNoEnum.YES.getCode());
		int taskCount4Finish = hiUserTaskDao.queryCount(taskQ);
		if(taskCount>0 && taskCount == taskCount4Finish){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int initTask4User(Integer userId, HiRuleTaskTypeDictEnum taskType) {
		HiRuleQuery ruleQ1 = new HiRuleQuery();
		ruleQ1.setClassify(HiRuleClassifyDictEnum.COMPLETE_NEW_TASK.getCode());//完成新任务后
		ruleQ1.setStatus(YesNoEnum.YES.getCode());
		Pageable pageKit1 = new PageKit(1,1, new PageOrder(Direction.ASC, "id"));
		List<HiRuleVo> ruleList1 = hiRuleDao.queryList(ruleQ1, pageKit1);
		int finishHonorValue = 0;
		int finishQuanValue = 0;
		if(ruleList1!=null && ruleList1.size()>0){
			HiRuleVo rule = ruleList1.get(0);
			if(rule!=null){
				finishHonorValue = rule.getHonorValue();//完成新后任务后额外得积分数
				finishQuanValue = rule.getQuanValue();//完成新后任务后额外得圈币数
			}
		}
		
		HiRuleQuery ruleQ = new HiRuleQuery();
		ruleQ.setTaskType(taskType.getCode());
		ruleQ.setStatus(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1,Integer.MAX_VALUE, new PageOrder(Direction.ASC, "id"));
		List<HiRuleVo> ruleList = hiRuleDao.queryList(ruleQ, pageKit);
		int addCount = 0;
		if(ruleList!=null && ruleList.size()>0){
			for(HiRuleVo rule: ruleList){
				if(rule.getClassify()!=HiRuleClassifyDictEnum.COMPLETE_NEW_TASK.getCode()){
					HiUserTask task = new HiUserTask();
					task.setUserId(userId);
					task.setRuleId(rule.getId());
					task.setRuleName(rule.getName());
					task.setIsFinish(YesNoEnum.NO.getCode());
					task.setFinishHonorValue(finishHonorValue);//完成所有任务时-奖励荣誉值
					task.setFinishQuanValue(finishQuanValue);//完成所有任务时-奖励圈币值
					task.setGroupType(rule.getGroupType());
					hiUserTaskDao.insert(task);
					addCount ++;
				}
			}
		}
		return addCount;
	}
	
	@Override
	public HiUserTaskVo getOneUserTask(Integer userId, Integer ruleId){
		HiUserTaskQuery taskQ = new HiUserTaskQuery();
		taskQ.setUserId(userId);
		taskQ.setRuleId(ruleId);
		Pageable pageKit = new PageKit(1,1, new PageOrder(Direction.ASC, "id"));
		List<HiUserTaskVo> ruleList = hiUserTaskDao.queryList(taskQ, pageKit);
		if(ruleList!=null && ruleList.size()>0){
			return ruleList.get(0);
		}else{
			HiUserTaskVo userTask = new HiUserTaskVo();
			userTask.setFinishHonorValue(0);
			userTask.setFinishQuanValue(0);
			return userTask;
		}
	}
}